//
//  ListCourseViewController.m
//  TMS
//
//  Created by Riddhi Joshi on 23/01/15.
//  Copyright (c) 2015 Riddhi Joshi. All rights reserved.
//

#import "ListCourseViewController.h"
#import "DBManager.h"
#import "CourseDetailViewController.h"
#import "CourseListViewController.h"

@interface ListCourseViewController ()

//@property (nonatomic, strong) DBManager *dbManager;

@property (nonatomic, strong) NSArray *arrCourseInfo;

@property (nonatomic) int recordIDToDisplay;

@property (nonatomic) int flagIDToDisplay;

-(void)loadData;
@end

@implementation ListCourseViewController
{
    NSArray *data;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
   
    
    self.edgesForExtendedLayout=UIRectEdgeNone;
    //self.dbManager = [[DBManager alloc] initWithDatabaseFilename:@"tmsdb.sql"];
    
    NSLog(@"In ListCourseViewController trainee id is=%d",self.TraineeId);
    
    [self loadData];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    if([segue.identifier isEqualToString:@"showCourseDetail"])
    {
    CourseDetailViewController *destViewController=[segue destinationViewController];
    destViewController.recordIDToDisplay=self.recordIDToDisplay;
    destViewController.TraineeId=self.TraineeId;
    destViewController.StatusId=self.flagIDToDisplay;
    destViewController.delegate=self;
    }
    
    else if([segue.identifier isEqualToString:@"showCourseList"])
    {
        CourseListViewController *destViewController=[segue destinationViewController];
        destViewController.TraineeId=self.TraineeId;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSLog(@"The array count is %lu",(unsigned long)self.arrCourseInfo.count);
    return self.arrCourseInfo.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *simpleTableIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
    }
    
   // NSInteger indexOfCourseId = [self.dbManager.arrColumnNames indexOfObject:@"cid"];
    NSInteger indexOfCourseName = [self.TraineeCourse.db.arrColumnNames indexOfObject:@"coursename"];
    NSInteger indexOfFlag = [self.TraineeCourse.db.arrColumnNames indexOfObject:@"flag"];
   // NSInteger indexofDate=   [self.TraineeCourse.db.arrColumnNames indexOfObject:@"date"];
    cell.textLabel.text = [NSString stringWithFormat:@"%@", [[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfCourseName]];
  
    int flagValue=[[[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfFlag] intValue];
    
    NSLog(@"Flag value %d",flagValue);
    
    NSString *status;
    switch (flagValue) {
        case 0:
            status=@"Not Accepted";
            break;
        case 1:
            status=@"Accepted";
            break;
        case 2:
            status=@"Rejected";
            break;
        default:
            break;
    }
    
    NSLog(@"Status is %@",status);
    
    //cell.detailTextLabel.text = [NSString stringWithFormat:@"%@ %@", [[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:indexOfFlag],[[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:indexofDate]];
    
    cell.detailTextLabel.text=status;
    return cell;
}


-(void)tableView:(UITableView *)tableView accessoryButtonTappedForRowWithIndexPath:(NSIndexPath *)indexPath{
    // Get the record ID of the selected name and set it to the recordIDToDisplay property.
    self.recordIDToDisplay = [[[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:0] intValue];
    
    self.flagIDToDisplay=[[[self.arrCourseInfo objectAtIndex:indexPath.row] objectAtIndex:2] intValue];
    NSLog(@"Record id in list Course view controller =%d",self.recordIDToDisplay);
     NSLog(@"Flag id in list Course view controller =%d",self.flagIDToDisplay);
    // Perform the segue.
    [self performSegueWithIdentifier:@"showCourseDetail" sender:self];
}

-(void)loadData{
    // Form the query.
 //   NSString *query = [NSString stringWithFormat:@"select Course.cid,Course.coursename,Trainee_Course.flag from Trainee_Course,Course where Trainee_Course.tid=1"];
    
    // Get the results.
    if (self.arrCourseInfo != nil) {
        self.arrCourseInfo = nil;
    }
    
    
    NSDate *date=[NSDate date];
    NSDateFormatter *dateformat=[[NSDateFormatter alloc]init];
    [dateformat setDateFormat:@"dd/MM/yyyy"];
    NSString *dateInString=[dateformat stringFromDate:date];
    
    NSLog(@"Current date %@",dateInString);
    
    
    self.TraineeCourse=[[TraineeCourseTable alloc]init];
    self.arrCourseInfo=[[NSArray alloc]init];
    self.arrCourseInfo=[self.TraineeCourse LoadCoursesWithTraineeID:self.TraineeId andDate:dateInString];
  //  self.arrCourseInfo = [[NSArray alloc] initWithArray:[self.dbManager loadDataFromDB:query]];
    
     NSUInteger count = self.arrCourseInfo.count;
    
    
    if (count<=0) {
        UIAlertView * alert =[[UIAlertView alloc ] initWithTitle:@"Message" message:@"No courses scheduled for today" delegate:self cancelButtonTitle:@"Ok" otherButtonTitles: nil];
        alert.alertViewStyle = UIAlertViewStyleDefault;
        //[alert addButtonWithTitle:@"Ok"];
        [alert show];
      
    }
    else
    {
        // Reload the table view.
        [self.tblCourse reloadData];
    }
    
}


-(void)approvingCourseInfoWasFinished{
    // Reload the data.
    [self loadData];
}

- (IBAction)Logout:(id)sender
{
    //on logout button pressed present the main login view back
    NSString * storyboardName = @"Main";
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:storyboardName bundle: nil];
    UIViewController * vc = [storyboard instantiateViewControllerWithIdentifier:@"LoginViewController"];
    
    [self presentViewController:vc animated:YES completion:nil];
}

- (IBAction)onList:(id)sender{
     [self performSegueWithIdentifier:@"showCourseList" sender:self];
}





@end
